package com.framework.web.restful.controller;

import com.framework.core.constant.Globals;
import com.framework.core.util.MD5Util;
import com.framework.web.restful.doamin.*;
import com.framework.web.sys.domain.User;
import com.framework.web.sys.service.LoginService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2016/5/18.
 */
@RestController
public class AppController {

    private static final Log log = LogFactory.getLog(AppController.class);
    private static Map<String, Integer> methodnamesMap = new HashMap<String, Integer>();

    static {
        methodnamesMap.put("App_Action_Login".toLowerCase(), new Integer(1));
    }

    @Autowired
    private LoginService loginService;

    @RequestMapping(method = RequestMethod.POST, value = "/appServiceCall", produces = "application/json")
    @ResponseBody
    public Object appCmd(@RequestBody AlarmAppServiceRequestInfo requestInfo,
                         HttpServletRequest request, HttpServletResponse response) {
        log.info("Client ip:" + ", method name:" + requestInfo.getMethod() + "  请求requestInfo.getId()==>" + requestInfo.getId());
        ServletContext application = request.getSession().getServletContext();
        Object returnObj = null;
        String method = requestInfo.getMethod();
        int m = getMethodToInt(method);
        switch (m) {
            //1.用户登录
            //用户登录 "method": "App_Action_Login",
            case 1:
                log.info(requestInfo.getParams().getUserName());
                User user = loginService.login(requestInfo.getParams().getUserName(), MD5Util.encodeStr(requestInfo.getParams().getPassword()));
                if (user != null) {
                    if (Globals.USER_FORBIDDEN.equals(user.getStatus())) {//禁用状态
                        CommonResponseErrorResult commonResponseErrorResult = new CommonResponseErrorResult();
                        CAError caError = new CAError();
                        caError.setCode(6);
                        caError.setMessage("用户已注销,请联系管理员");
                        commonResponseErrorResult.setError(caError);
                        commonResponseErrorResult.setId(requestInfo.getId());
                        returnObj = commonResponseErrorResult;
                        break;
                    } else {
                        RestFulResult<ApiActionLogin> ret = new RestFulResult<ApiActionLogin>();
                        //登陆信息
                        ApiActionLogin result = new ApiActionLogin();
                        result.setUserGuid(UUID.randomUUID().toString());
                        result.setUserId(user.getId());
                        result.setUserName(user.getUserName());
                        ret.setResult(result);
                        returnObj = ret;
                    }
                } else {
                    CommonResponseErrorResult commonResponseErrorResult = new CommonResponseErrorResult();
                    CAError caError = new CAError();
                    caError.setCode(2);
                    caError.setMessage("用户或密码错误");
                    commonResponseErrorResult.setError(caError);
                    commonResponseErrorResult.setId(requestInfo.getId());
                    returnObj = commonResponseErrorResult;
                }

        }
        return returnObj;
    }

    public int getMethodToInt(String method) {
        Integer index = methodnamesMap.get(method.toLowerCase());
        if (index == null) {
            return 999;
        }
        return index.intValue();
    }

}
